package android.support.v4.media.session;

import android.content.Context;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Message;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat.QueueItem;
import android.support.v4.media.session.MediaSessionCompat.Token;
import android.util.Log;
import android.view.KeyEvent;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import o.fh;
import o.u;
import o.w;
import o.x;

public final class MediaControllerCompat {
    private final Token ˋ;
    private final a ˎ;
    private final HashSet<c> ˏ = new HashSet();

    interface a {
        boolean ˎ(KeyEvent keyEvent);
    }

    @RequiresApi(21)
    static class MediaControllerImplApi21 implements a {
        final Object ˊ = new Object();
        protected final Object ˋ;
        @GuardedBy("mLock")
        private final List<c> ˎ = new ArrayList();
        private HashMap<c, b> ˏ = new HashMap();
        final Token ॱ;

        static class ExtraBinderRequestResultReceiver extends ResultReceiver {
            private WeakReference<MediaControllerImplApi21> ॱ;

            ExtraBinderRequestResultReceiver(MediaControllerImplApi21 mediaControllerImplApi21) {
                super(null);
                this.ॱ = new WeakReference(mediaControllerImplApi21);
            }

            protected void onReceiveResult(int i, Bundle bundle) {
                MediaControllerImplApi21 mediaControllerImplApi21 = (MediaControllerImplApi21) this.ॱ.get();
                if (mediaControllerImplApi21 != null && bundle != null) {
                    synchronized (mediaControllerImplApi21.ˊ) {
                        mediaControllerImplApi21.ॱ.ॱ(o.u.e.ˋ(fh.ˏ(bundle, "android.support.v4.media.session.EXTRA_BINDER")));
                        mediaControllerImplApi21.ॱ.ˋ(bundle.getBundle("android.support.v4.media.session.SESSION_TOKEN2_BUNDLE"));
                        mediaControllerImplApi21.ˊ();
                    }
                }
            }
        }

        static class b extends e {
            b(c cVar) {
                super(cVar);
            }

            public void ˏ() throws RemoteException {
                throw new AssertionError();
            }

            public void ˊ(MediaMetadataCompat mediaMetadataCompat) throws RemoteException {
                throw new AssertionError();
            }

            public void ˎ(List<QueueItem> list) throws RemoteException {
                throw new AssertionError();
            }

            public void ˊ(CharSequence charSequence) throws RemoteException {
                throw new AssertionError();
            }

            public void ॱ(Bundle bundle) throws RemoteException {
                throw new AssertionError();
            }

            public void ˏ(ParcelableVolumeInfo parcelableVolumeInfo) throws RemoteException {
                throw new AssertionError();
            }
        }

        public MediaControllerImplApi21(Context context, Token token) throws RemoteException {
            this.ॱ = token;
            this.ˋ = x.ˊ(context, this.ॱ.ॱ());
            if (this.ˋ == null) {
                throw new RemoteException();
            } else if (this.ॱ.ˎ() == null) {
                ˏ();
            }
        }

        public boolean ˎ(KeyEvent keyEvent) {
            return x.ˎ(this.ˋ, keyEvent);
        }

        public void ˋ(String str, Bundle bundle, ResultReceiver resultReceiver) {
            x.ˋ(this.ˋ, str, bundle, resultReceiver);
        }

        private void ˏ() {
            ˋ("android.support.v4.media.session.command.GET_EXTRA_BINDER", null, new ExtraBinderRequestResultReceiver(this));
        }

        @GuardedBy("mLock")
        void ˊ() {
            if (this.ॱ.ˎ() != null) {
                for (c cVar : this.ˎ) {
                    w bVar = new b(cVar);
                    this.ˏ.put(cVar, bVar);
                    cVar.ˋ = bVar;
                    try {
                        this.ॱ.ˎ().ˋ(bVar);
                        cVar.ˋ(13, null, null);
                    } catch (Throwable e) {
                        Log.e("MediaControllerCompat", "Dead object in registerCallback.", e);
                    }
                }
                this.ˎ.clear();
            }
        }
    }

    static class b implements a {
        private u ˊ;

        public b(Token token) {
            this.ˊ = o.u.e.ˋ((IBinder) token.ॱ());
        }

        public boolean ˎ(KeyEvent keyEvent) {
            if (keyEvent == null) {
                throw new IllegalArgumentException("event may not be null.");
            }
            try {
                this.ˊ.ॱ(keyEvent);
            } catch (Throwable e) {
                Log.e("MediaControllerCompat", "Dead object in dispatchMediaButtonEvent.", e);
            }
            return false;
        }
    }

    public static abstract class c implements DeathRecipient {
        final Object ˊ;
        w ˋ;
        a ˏ;

        static class e extends o.w.b {
            private final WeakReference<c> ˊ;

            e(c cVar) {
                this.ˊ = new WeakReference(cVar);
            }

            public void ˎ(String str, Bundle bundle) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(1, str, bundle);
                }
            }

            public void ˏ() throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(8, null, null);
                }
            }

            public void ॱ(PlaybackStateCompat playbackStateCompat) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(2, playbackStateCompat, null);
                }
            }

            public void ˊ(MediaMetadataCompat mediaMetadataCompat) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(3, mediaMetadataCompat, null);
                }
            }

            public void ˎ(List<QueueItem> list) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(5, list, null);
                }
            }

            public void ˊ(CharSequence charSequence) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(6, charSequence, null);
                }
            }

            public void ˏ(boolean z) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(11, Boolean.valueOf(z), null);
                }
            }

            public void ˊ(int i) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(9, Integer.valueOf(i), null);
                }
            }

            public void ˋ(boolean z) throws RemoteException {
            }

            public void ॱ(int i) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(12, Integer.valueOf(i), null);
                }
            }

            public void ॱ(Bundle bundle) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(7, bundle, null);
                }
            }

            public void ˏ(ParcelableVolumeInfo parcelableVolumeInfo) throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    Object iVar;
                    if (parcelableVolumeInfo != null) {
                        iVar = new i(parcelableVolumeInfo.ॱ, parcelableVolumeInfo.ˊ, parcelableVolumeInfo.ˏ, parcelableVolumeInfo.ˋ, parcelableVolumeInfo.ˎ);
                    } else {
                        iVar = null;
                    }
                    cVar.ˋ(4, iVar, null);
                }
            }

            public void ˎ() throws RemoteException {
                c cVar = (c) this.ˊ.get();
                if (cVar != null) {
                    cVar.ˋ(13, null, null);
                }
            }
        }

        class a extends Handler {
            final /* synthetic */ c ˊ;
            boolean ˏ;

            public void handleMessage(Message message) {
                if (this.ˏ) {
                    switch (message.what) {
                        case 1:
                            Bundle data = message.getData();
                            MediaSessionCompat.ˏ(data);
                            this.ˊ.ˊ((String) message.obj, data);
                            return;
                        case 2:
                            this.ˊ.ˋ((PlaybackStateCompat) message.obj);
                            return;
                        case 3:
                            this.ˊ.ˋ((MediaMetadataCompat) message.obj);
                            return;
                        case 4:
                            this.ˊ.ॱ((i) message.obj);
                            return;
                        case 5:
                            this.ˊ.ˎ((List) message.obj);
                            return;
                        case 6:
                            this.ˊ.ˊ((CharSequence) message.obj);
                            return;
                        case 7:
                            Bundle bundle = (Bundle) message.obj;
                            MediaSessionCompat.ˏ(bundle);
                            this.ˊ.ˋ(bundle);
                            return;
                        case 8:
                            this.ˊ.ˏ();
                            return;
                        case 9:
                            this.ˊ.ˊ(((Integer) message.obj).intValue());
                            return;
                        case 11:
                            this.ˊ.ॱ(((Boolean) message.obj).booleanValue());
                            return;
                        case 12:
                            this.ˊ.ˏ(((Integer) message.obj).intValue());
                            return;
                        case 13:
                            this.ˊ.ˊ();
                            return;
                        default:
                            return;
                    }
                }
            }
        }

        static class b implements o.x.d {
            private final WeakReference<c> ॱ;

            b(c cVar) {
                this.ॱ = new WeakReference(cVar);
            }

            public void ˏ() {
                c cVar = (c) this.ॱ.get();
                if (cVar != null) {
                    cVar.ˏ();
                }
            }

            public void ˏ(String str, Bundle bundle) {
                c cVar = (c) this.ॱ.get();
                if (cVar == null) {
                    return;
                }
                if (cVar.ˋ == null || VERSION.SDK_INT >= 23) {
                    cVar.ˊ(str, bundle);
                }
            }

            public void ˊ(Object obj) {
                c cVar = (c) this.ॱ.get();
                if (cVar != null && cVar.ˋ == null) {
                    cVar.ˋ(PlaybackStateCompat.ˋ(obj));
                }
            }

            public void ˎ(Object obj) {
                c cVar = (c) this.ॱ.get();
                if (cVar != null) {
                    cVar.ˋ(MediaMetadataCompat.ˏ(obj));
                }
            }

            public void ˏ(List<?> list) {
                c cVar = (c) this.ॱ.get();
                if (cVar != null) {
                    cVar.ˎ(QueueItem.ˋ(list));
                }
            }

            public void ॱ(CharSequence charSequence) {
                c cVar = (c) this.ॱ.get();
                if (cVar != null) {
                    cVar.ˊ(charSequence);
                }
            }

            public void ˎ(Bundle bundle) {
                c cVar = (c) this.ॱ.get();
                if (cVar != null) {
                    cVar.ˋ(bundle);
                }
            }

            public void ˎ(int i, int i2, int i3, int i4, int i5) {
                c cVar = (c) this.ॱ.get();
                if (cVar != null) {
                    cVar.ॱ(new i(i, i2, i3, i4, i5));
                }
            }
        }

        public c() {
            if (VERSION.SDK_INT >= 21) {
                this.ˊ = x.ˎ(new b(this));
                return;
            }
            w eVar = new e(this);
            this.ˋ = eVar;
            this.ˊ = eVar;
        }

        public void ˊ() {
        }

        public void ˏ() {
        }

        public void ˊ(String str, Bundle bundle) {
        }

        public void ˋ(PlaybackStateCompat playbackStateCompat) {
        }

        public void ˋ(MediaMetadataCompat mediaMetadataCompat) {
        }

        public void ˎ(List<QueueItem> list) {
        }

        public void ˊ(CharSequence charSequence) {
        }

        public void ˋ(Bundle bundle) {
        }

        public void ॱ(i iVar) {
        }

        public void ॱ(boolean z) {
        }

        public void ˊ(int i) {
        }

        public void ˏ(int i) {
        }

        void ˋ(int i, Object obj, Bundle bundle) {
            if (this.ˏ != null) {
                Message obtainMessage = this.ˏ.obtainMessage(i, obj);
                obtainMessage.setData(bundle);
                obtainMessage.sendToTarget();
            }
        }
    }

    @RequiresApi(23)
    static class d extends MediaControllerImplApi21 {
        public d(Context context, Token token) throws RemoteException {
            super(context, token);
        }
    }

    @RequiresApi(24)
    static class e extends d {
        public e(Context context, Token token) throws RemoteException {
            super(context, token);
        }
    }

    public static final class i {
        private final int ˊ;
        private final int ˋ;
        private final int ˎ;
        private final int ˏ;
        private final int ॱ;

        i(int i, int i2, int i3, int i4, int i5) {
            this.ˏ = i;
            this.ˎ = i2;
            this.ॱ = i3;
            this.ˋ = i4;
            this.ˊ = i5;
        }
    }

    public MediaControllerCompat(Context context, @NonNull Token token) throws RemoteException {
        if (token == null) {
            throw new IllegalArgumentException("sessionToken must not be null");
        }
        this.ˋ = token;
        if (VERSION.SDK_INT >= 24) {
            this.ˎ = new e(context, token);
        } else if (VERSION.SDK_INT >= 23) {
            this.ˎ = new d(context, token);
        } else if (VERSION.SDK_INT >= 21) {
            this.ˎ = new MediaControllerImplApi21(context, token);
        } else {
            this.ˎ = new b(token);
        }
    }

    public boolean ˋ(KeyEvent keyEvent) {
        if (keyEvent != null) {
            return this.ˎ.ˎ(keyEvent);
        }
        throw new IllegalArgumentException("KeyEvent may not be null");
    }
}
